package club.dataresources.controller;

import club.dataresources.common.R;
import club.dataresources.entity.Order;
import club.dataresources.mapper.OrderMapper;
import club.dataresources.service.MenuService;
import club.dataresources.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户下订单 服务
 *
 * @author
 * @create 2018-11-12 10:58
 **/
@RequestMapping(value = "/order")
@Controller
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private MenuService menuService;

    @Autowired
    private OrderMapper orderMapper;

    @RequestMapping("addOrder")
    public @ResponseBody
    R addOrder(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestBody Order order){
        Order newOrder = new Order();
        newOrder.setOrderStatus("开始");
        newOrder.setOrderDetail(order.getMenuId().toString());
        newOrder.setOrderPrice(699f);
        newOrder.setWaiterId(1);
        newOrder.setBarberId(order.getBarberId());
        newOrder.setMenuId(order.getMenuId());
        newOrder.setCustomerId(1);
        newOrder.setOrderNum("00001");
        newOrder.setBarberorderedIs(1);
        newOrder.setBarberorderedTime(order.getBarberorderedTime());
        try{
            orderMapper.insertSelective(newOrder);
            return R.ok().put("code",200).put("data",order);
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }


    /**
     * 用户选择套餐下订单
     * 后台将添加初步的订单记录
     * @param menuId
     * @return
     */
    @RequestMapping("/getMenuOrder")
    public @ResponseBody
    R getMenuOrder(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam("menuId") Integer menuId){
        System.out.println("================用户选择套餐下订单====================");
        try {
            Integer id = (Integer)request.getSession().getAttribute("customerId");
            if(id!=null && !id.equals(" ")){
                Order order = orderService.getMenuOrder(menuId,id);
                if(orderService.addOrder(order)){
                    request.getSession().setAttribute("order",order);
                    request.getRequestDispatcher("/checkMenuOrder").forward(request,response);
                    return R.ok().put("code",200).put("msg","套餐已加入订单");
                }else{
                    return R.error().put("code",500).put("msg","套餐加入订单失败");
                }
            }else {
                return R.error().put("code",500).put("msg","customerId为null");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",200).put("msg","后台服务器异常");
        }
    }

    /**
     * 展示该用户最新的一个订单详情
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/checkMenuOrder")
    public @ResponseBody
    R checkMenuOrder(
            HttpServletRequest request,
            HttpServletResponse response){
        System.out.println("================展示该用户最新的一个订单详情====================");
        try{
            Order order = (Order)request.getAttribute("order");
            Order returnOrder = orderService.checkMenuOrder(order);
            if(returnOrder==null){
                return R.error().put("code",500).put("msg","订单记录生成错误,为空");
            }else{
                return R.ok().put("code",200).put("data",returnOrder).put("msg","显示订单完毕");
            }
        }catch (Exception e){
            return R.error().put("code",500).put("msg","后台服务器错误");
        }
    }

    /**
     * 用户在订单详情页面中点击 修改理发师 按钮
     * 请求转发到浏览理发师页面，获取新的barberId
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("changeBarberForOrder")
    public @ResponseBody
    R changeBarberForOrder(
            HttpServletRequest request,
            HttpServletResponse response){
        System.out.println("================在订单详情中用户更改理发师的选择====================");
        try{
            request.getRequestDispatcher("/subscribe/listBarber").forward(request,response);
            return R.ok().put("code",200).put("msg","请求转发到浏览理发师页面成功");
        }catch (Exception e){
            return R.error().put("code",500).put("msg","后台服务器异常:请求转发到浏览理发师页面失败");
        }
    }
}
